#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>
#include <stdio.h>

void gtTrip(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[0];
    Iset = gtrandevice->gtranSetValue[0];

    Ia = gtrandevice->halfphasor[3];
    Ib = gtrandevice->halfphasor[4];
    Ic = gtrandevice->halfphasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && ((time - startTime) > 0.01)){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "速断过流保护动作");
    }
}

void gtOver1(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[1];
    Iset = gtrandevice->gtranSetValue[1];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && ((time - startTime) > (0.02 + gtrandevice->gtranTimeSet[1]))){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "过流保护I段动作");
    }

}

void gtOver2(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic;
    double Imax;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[2];
    Iset = gtrandevice->gtranSetValue[2];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    Imax = phasorAbs(Ia);
    if(Imax < phasorAbs(Ib)){
        Imax = phasorAbs(Ib);
    }
    if(Imax < phasorAbs(Ic)){
        Imax = phasorAbs(Ic);
    }

    if((Imax > Iset) && ((time - startTime) > (0.02 + gtrandevice->gtranTimeSet[2]))){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "过流保护II段动作");
    }

}

void gtZero1(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[3];
    Iset = gtrandevice->gtranSetValue[3];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > Iset) && ((time - startTime) > (0.02 + gtrandevice->gtranTimeSet[3]))){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "高压侧接地保护I段动作");
    }

}


void gtZero2(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[4];
    Iset = gtrandevice->gtranSetValue[4];

    Ia = gtrandevice->phasor[3];
    Ib = gtrandevice->phasor[4];
    Ic = gtrandevice->phasor[5];

    I0 = phasorAdd(Ia, Ib);
    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > Iset) && ((time - startTime) > (0.02 + gtrandevice->gtranTimeSet[4]))){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "高压侧接地保护II段动作");
    }

}


void gtLow(gtranDevice* gtrandevice){
    Phasor Ia, Ib, Ic, I0;
    double Iset;
    double time, startTime;
    time = gtrandevice->time;
    startTime = gtrandevice->startTime[5];
    Iset = gtrandevice->gtranSetValue[5];

    I0 = gtrandevice->phasor[6];

//    Ia = gtrandevice->phasor[6];
//    Ib = gtrandevice->phasor[7];
//    Ic = gtrandevice->phasor[8];
//
//    I0 = phasorAdd(Ia, Ib);
//    I0 = phasorAdd(I0, Ic);

    if((phasorAbs(I0) > Iset) && ((time - startTime) > (0.02 + gtrandevice->gtranTimeSet[5]))){
        gtrandevice->mainFlag = 1;
        gtrandevice->gtranProRes[0] = 1;
        gtranWriteLog(gtrandevice, "低压侧零序过流保护动作");
    }

}